<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/11/6 14:35
 */

namespace App\Transformers\Api;

use App\Models\Patient;
use Illuminate\Support\Str;
use League\Fractal\TransformerAbstract;

class PatientTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['records', 'physique'];

    public function transform(Patient $patient)
    {
        //  病历信息
        $transform = [
            'id'              => $patient->id,
            'name'            => $patient->name,
            'sex'             => $patient->sex,
            'phone'           => $patient->phone,
            'hospital_id' => $patient->hospital_id,
            'birthday'        => $patient->birthday,
            'medical_number'  => $patient->medical_number,
            'diabetes_type'   => $patient->diabetes_type,
            'special_illness' => $patient->special_illness,
            'cardnumber'      => $patient->CARDNUMBER,
            'nation'          => $patient->nation,
            'province'        => $patient->province,
            'city'            => $patient->city,
            'county'          => $patient->county,
            'invite_code'     => $patient->INVITE_CODE,
            'huan_username'   => 'patient_' . $patient->account,
        ];

        return $transform;
    }

    /**
     * 看诊信息记录
     * @param \App\Models\Patient $patient
     * @return \League\Fractal\Resource\Collection
     */
    public function includeRecords(Patient $patient)
    {
        return $this->collection(
            $patient->records(),
            new MedicalRecordTransformer()
        );
    }

    public function includePhysique(Patient $patient)
    {
        return $this->item($patient, function (Patient $patient) {

            $data = (array)\DB::table('tn_patient')->where('ACCOUNT', $patient->account)->first();

            if ($patient->birthday && ($patient->birthday != "000-00-00")) {
                $age = $this->calcAge($patient->birthday);
                if(!$age){
                    $age = 0;
                }
                $data["AGE"] = $age;
            } else {
                $data["AGE"] = $patient->age;
            }

            $data["SEX"] = $patient->sex['key'];

            if(empty($data['PIC'])){
                $data['PIC'] = 'http://www.365tang.cn/upload/face/user_man.png';
            }else{
                if (!Str::startsWith($data['PIC'], 'http')) {
                    $data['PIC'] = 'http://www.365tang.cn/upload/face/' . $data['PIC'];
                }
            }

            return $data;
        });
    }

    /**
     * @uses 根据生日计算年龄，年龄的格式是：2016-09-23
     * @param string $birthday
     * @return string|number
     */
    public function calcAge($birthday) {
        $iage = 0;
        if (!empty($birthday)) {
            $year = date('Y',strtotime($birthday));
            $month = date('m',strtotime($birthday));
            $day = date('d',strtotime($birthday));

            $now_year = date('Y');
            $now_month = date('m');
            $now_day = date('d');

            if ($now_year > $year) {
                $iage = $now_year - $year - 1;
                if ($now_month > $month) {
                    $iage++;
                } else if ($now_month == $month) {
                    if ($now_day >= $day) {
                        $iage++;
                    }
                }
            }
        }
        return $iage;
    }


}